package com.eqdd.yiqidian.ui.mine.wo;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.amap.api.maps2d.model.LatLng;
import com.eqdd.floatingmenu.MenuItem;
import com.eqdd.floatingmenu.TopRightMenu;
import com.eqdd.yiqidian.CollectionActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.DividerItemDecoration;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CollectionAudioBean;
import com.eqdd.yiqidian.bean.rebuild.CollectionBase;
import com.eqdd.yiqidian.bean.rebuild.CollectionImgBean;
import com.eqdd.yiqidian.bean.rebuild.CollectionImgUrlWithTextBean;
import com.eqdd.yiqidian.bean.rebuild.CollectionImgWithTextBean;
import com.eqdd.yiqidian.bean.rebuild.CollectionLocationBean;
import com.eqdd.yiqidian.bean.rebuild.CollectionTextBean;
import com.eqdd.yiqidian.bean.rebuild.ShareConf;
import com.eqdd.yiqidian.bean.rebuild.ShareEnterConf;
import com.eqdd.yiqidian.bean.rebuild.ZhangPageResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.map.SelectMapActivity;
import com.eqdd.yiqidian.map.ShowMapActivity;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.mine.FriendCircleSettingActivity;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.ui.select.RichTextResult;
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity;
import com.eqdd.yiqidian.ui.share.JiaoziVideoPlayActivity;
import com.eqdd.yiqidian.ui.share.ShareEnterActivity;
import com.eqdd.yiqidian.ui.share.VideoPlayActivity;
import com.eqdd.yiqidian.utils.IntentUtil;
import com.eqdd.yiqidian.utils.PermissionTipUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.gamerole.audiorecordlib.AndroidAudioRecorder;
import com.gamerole.audiorecordlib.model.AudioChannel;
import com.gamerole.audiorecordlib.model.AudioSampleRate;
import com.gamerole.audiorecordlib.model.AudioSource;
import com.jakewharton.rxbinding.view.RxView;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCCoolHelper;
import com.tbruyelle.rxpermissions.RxPermissions;

import org.reactivestreams.Publisher;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cafe.adriel.androidaudioconverter.AndroidAudioConverter;
import cafe.adriel.androidaudioconverter.callback.IConvertCallback;
import cafe.adriel.androidaudioconverter.model.AudioFormat;
import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

public class CollectionActivity extends BaseActivity {
    private CollectionActivityCustom dataBinding;
    private MVCCoolHelper<List<CollectionBase>> mvcHelper;
    private ModelRx2DataSource<CollectionBase> dataSource;
    private static final String AUDIO_FILE_PATH =
            Environment.getExternalStorageDirectory().getPath() + "/recorded_audio.wav";
    private static final String AUDIO_FILE_PATH_MP3 =
            Environment.getExternalStorageDirectory().getPath() + "/recorded_audio.mp3";
    private int pageNum;
    private int group = 0;
    private SlimAdapterEx<CollectionBase> slimAdapterEx;
    private TopRightMenu mTopRightMenu;
    private RichTextResult richTextResult;
    private String totalTime;
    private int movePos;
    private String audioSize;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_collection);
        initTopTitleBar(View.VISIBLE, "我的收藏");
        RxView.clicks(dataBinding.includeTitle.tvTitle)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(aVoid -> {
                    startActivityForResult(SelectCollectionGroupActivity.class, null, Config.SELECT_COLLECTION_GROUP);

                });
        initTopRightText("添加", v -> {
            mTopRightMenu = new TopRightMenu(CollectionActivity.this);

//添加菜单项
            List<MenuItem> menuItems = new ArrayList<>();
            menuItems.add(new MenuItem("图文"));
            menuItems.add(new MenuItem("语音"));
            menuItems.add(new MenuItem("位置"));

            mTopRightMenu
                    .setHeight(300)     //默认高度480
                    .setWidth(250)      //默认宽度wrap_content
                    .showIcon(false)     //显示菜单图标，默认为true
                    .dimBackground(true)        //背景变暗，默认为true
                    .needAnimationStyle(true)   //显示动画，默认为true
                    .setAnimationStyle(R.style.TRM_ANIM_STYLE)
                    .addMenuList(menuItems)
                    .top(TopRightMenu.Align.RIGHT, 10, 30)
                    .setOnMenuItemClickListener(position -> {
                        switch (position) {
                            case 0:
                                Bundle bundle = new Bundle();
                                bundle.putString(Config.TITLE, "图文");
                                bundle.putBoolean(Config.TEXT_BLANK, true);
                                startActivityForResult(SelectRichTextActivity.class, bundle, Config.SELECT_RICH_TEXT);
                                break;
                            case 1:
                                RxPermissions.getInstance(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO)
                                        .subscribe(isGrant -> {
                                            if (isGrant) {
                                                AndroidAudioRecorder.with(this)
                                                        // Required
                                                        .setFilePath(AUDIO_FILE_PATH)
                                                        .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
                                                        .setRequestCode(Config.RECORD)

                                                        // Optional
                                                        .setSource(AudioSource.MIC)
                                                        .setChannel(AudioChannel.STEREO)
                                                        .setSampleRate(AudioSampleRate.HZ_48000)
                                                        .setAutoStart(false)
                                                        .setKeepDisplayOn(true)

                                                        // Start recording
                                                        .record();
                                            } else {
                                                PermissionTipUtil.tip(this, "存储和录音");
                                            }
                                        });

                                break;
                            case 2:
                                startActivityForResult(SelectMapActivity.class, null, Config.SELECT_MAP);
                                break;
                        }
                    })
                    .showAsDropDown(v, 0, -20);    //带偏移量
        });
    }

    @Override
    public void initData() {


    }

    @Override
    public void setView() {

        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, Color.LTGRAY, 10));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_68, new SlimInjector<CollectionTextBean>() {
            @Override
            public void onInject(CollectionTextBean data, IViewInjector injector) {

                injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                        .text(R.id.tv_name, data.getSourceOwner().getNickname())
                        .text(R.id.tv_time, data.getCollection().getCreateTime())
                        .text(R.id.tv_from, data.getCollection().getSource())
                        .text(R.id.tv_content, data.getCollection().getContent());
            }
        })
                .register(R.layout.list_item_69, new SlimInjector<CollectionImgBean>() {
                    @Override
                    public void onInject(CollectionImgBean data, IViewInjector injector) {
                        injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                                .text(R.id.tv_name, data.getSourceOwner().getNickname())
                                .text(R.id.tv_time, data.getCollection().getCreateTime())
                                .text(R.id.tv_from, data.getCollection().getSource());
                        RecyclerView recyclerView = (RecyclerView) injector.findViewById(R.id.recyclerView);
                        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, 5, false));
                        recyclerView.setLayoutManager(new GridLayoutManager(CollectionActivity.this, 3));
                        SlimAdapterEx.create().register(R.layout.list_item_friend_circle_img, new SlimInjector<String>() {
                            @Override
                            public void onInject(String data, IViewInjector injector) {
                                injector.imageRound(R.id.iv_img, data);
                            }
                        }).attachTo(recyclerView).updateData(Arrays.asList(data.getCollection().getUrls()).subList(0,
                                data.getCollection().getUrls().length > 3 ? 3 : data.getCollection().getUrls().length));
                        ItemClickSupport.addTo(recyclerView)
                                .setOnItemClickListener((recyclerView1, position, v) -> {
                                    ArrayList<LocalMedia> localMedia = new ArrayList<>(data.getCollection().getUrls().length);
                                    for (String s : data.getCollection().getUrls()) {
                                        localMedia.add(PicUtil.localMedia(s));
                                    }
                                    PicUtil.preview(CollectionActivity.this, position, localMedia);
                                });
                    }
                })
                .register(R.layout.list_item_70, new SlimInjector<CollectionImgWithTextBean>() {
                    @Override
                    public void onInject(CollectionImgWithTextBean data, IViewInjector injector) {
                        injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                                .text(R.id.tv_name, data.getSourceOwner().getNickname())
                                .text(R.id.tv_time, data.getCollection().getCreateTime())
                                .text(R.id.tv_from, data.getCollection().getSource())
                                .imageRound(R.id.iv_img, data.getCollection().getUrls()[0])
                                .text(R.id.tv_content, data.getCollection().getContent())
                                .clicked(R.id.iv_img, v -> {
                                    ArrayList<LocalMedia> localMedia = new ArrayList<>(data.getCollection().getUrls().length);
                                    for (String s : data.getCollection().getUrls()) {
                                        localMedia.add(PicUtil.localMedia(s));
                                    }
                                    PicUtil.preview(CollectionActivity.this, 0, localMedia);
                                });


                    }
                }).register(R.layout.list_item_70, new SlimInjector<CollectionImgUrlWithTextBean>() {
                    @Override
                    public void onInject(CollectionImgUrlWithTextBean data, IViewInjector injector) {
                        String url = data.getCollection().getUrl();
                        String[] split = url.substring(0, url.lastIndexOf(";")).split(";");
                        injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                                .text(R.id.tv_name, data.getSourceOwner().getNickname())
                                .text(R.id.tv_time, data.getCollection().getCreateTime())
                                .text(R.id.tv_from, data.getCollection().getSource())
                                .imageRound(R.id.iv_img, split[0])
                                .text(R.id.tv_content, data.getCollection().getContent())
                                .clicked(R.id.iv_img, v -> {
                                    ArrayList<LocalMedia> localMedia = new ArrayList<>(split.length);
                                    for (String s : split) {
                                        localMedia.add(PicUtil.localMedia(s));
                                    }
                                    PicUtil.preview(CollectionActivity.this, 0, localMedia);
                                });


                    }
                }).register(R.layout.list_item_71, new SlimInjector<CollectionAudioBean>() {
                    @Override
                    public void onInject(CollectionAudioBean data, IViewInjector injector) {
                        injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                                .text(R.id.tv_name, data.getSourceOwner().getNickname())
                                .text(R.id.tv_time, data.getCollection().getCreateTime())
                                .text(R.id.tv_from, data.getCollection().getSource())
                                .image(R.id.iv_img, R.mipmap.voice_record)
                                .text(R.id.tv_content, data.getCollection().getContent());

                    }
                }).register(R.layout.list_item_71, new SlimInjector<CollectionLocationBean>() {
                    @Override
                    public void onInject(CollectionLocationBean data, IViewInjector injector) {
                        injector.imageCircle(R.id.iv_head, data.getSourceOwner().getAvatar())
                                .text(R.id.tv_name, data.getSourceOwner().getNickname())
                                .text(R.id.tv_time, data.getCollection().getCreateTime())
                                .text(R.id.tv_from, data.getCollection().getSource())
                                .image(R.id.iv_img, R.mipmap.ic_location)
                                .text(R.id.tv_content, data.getCollection().getContent());

                    }
                })
                .attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    CollectionBase dataItem = slimAdapterEx.getDataItem(position);
                    if (dataItem instanceof CollectionLocationBean) {
                        Bundle bundle = new Bundle();
                        String[] split = dataItem.getCollection().getPosition().split(",");
                        bundle.putDouble(Config.LAT, Double.parseDouble(split[0]));
                        bundle.putDouble(Config.LON, Double.parseDouble(split[1]));
                        bundle.putString(Config.AREA_CONTENT, dataItem.getCollection().getContent());
                        startActivity(ShowMapActivity.class, bundle);
                    } else if (dataItem instanceof CollectionAudioBean) {
//                        playAudio(dataItem.getCollection().getUrls()[0]);
                        Bundle bundle = new Bundle();
                        bundle.putString(Config.VIDEO_URL, dataItem.getCollection().getUrls()[0]);
//                        startActivity(VideoPlayActivity.class, bundle);
                        startActivity(JiaoziVideoPlayActivity.class, bundle);
                    }
                })
                .setOnItemLongClickListener((recyclerView, position, v) -> {
                    ChooseAlertView.getBottomChoose(this, null, new String[]{"移动至分组", "删除"}, (o, pos) -> {

                        if (pos == 1) {
                            movePos = position;
                            startActivityForResult(SelectCollectionGroupActivity.class, null, Config.MOVE_COLLECTION_GROUP);

                        } else if (pos == 2) {
                            delete(position);
                        }
                    }).show();

                    return true;
                });
        mvcHelper = new MVCCoolHelper<>(dataBinding.coolRefreshView);
        dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSourcce(int page, Rx2DataSource.DoneActionRegister register) {
                return OkGo.<ZhangPageResultBean<List<CollectionBase>>>post(HttpConfig.BASE_URL + HttpConfig.COLLECTION_LIST)
                        .params("owner", AppConstent.user.getGuid())
                        .params("group", group)
                        .params("page", pageNum)
                        .converter(new JsonConverter<ZhangPageResultBean<List<CollectionBase>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }
                        })
                        .adapt(new FlowableBody<ZhangPageResultBean<List<CollectionBase>>>())
                        .flatMap(new Function<ZhangPageResultBean<List<CollectionBase>>, Publisher<List<CollectionBase>>>() {
                            @Override
                            public Publisher<List<CollectionBase>> apply(@NonNull ZhangPageResultBean<List<CollectionBase>> listZhangPageResultBean) throws Exception {
                                pageNum = page == 1 ? 0 : listZhangPageResultBean.getNextpage();

                                return Flowable.fromArray(listZhangPageResultBean.getItems())
                                        .map(new Function<List<CollectionBase>, List<CollectionBase>>() {
                                            @Override
                                            public List<CollectionBase> apply(List<CollectionBase> collectionBases) throws Exception {
                                                ArrayList<CollectionBase> data = new ArrayList<>();
                                                for (CollectionBase collectionBasis : collectionBases) {
                                                    switch (collectionBasis.getCollection().getType()) {
                                                        case 1:
                                                            data.add(new CollectionImgBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 2:
                                                            data.add(new CollectionTextBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 3:
                                                        case 4:
                                                            data.add(new CollectionImgWithTextBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 5:
                                                            data.add(new CollectionAudioBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 6:
                                                            data.add(new CollectionLocationBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 8:
                                                            data.add(new CollectionImgBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                        case 9:
                                                            data.add(new CollectionImgUrlWithTextBean(collectionBasis.getCollection(), collectionBasis.getSourceOwner()));
                                                            break;
                                                    }

                                                }

                                                return data;
                                            }
                                        });
                            }
                        });
            }
        }, 20);
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.refresh();


    }

    private void delete(int position) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_COLLECTION)
                .params("owner", user.getGuid())
                .params("collections", slimAdapterEx.getDataItem(position).getCollection().getId())
                .execute(new DialogCallBack<HttpResult>(CollectionActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.getData().remove(position);
                            slimAdapterEx.notifyItemRemoved(position);
                        }
                    }
                });
    }

    /**
     * 播放指定名称的歌曲
     *
     * @param audioPath 指定默认播放的音乐
     */
    public void playAudio(String audioPath) {
        Intent mIntent = new Intent();
        mIntent.setAction(android.content.Intent.ACTION_VIEW);
        Uri uri = Uri.parse(audioPath);//替换成audiopath
        mIntent.setDataAndType(uri, "audio/*");
        startActivity(mIntent);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_RICH_TEXT:
                    richTextResult = (RichTextResult) data.getSerializableExtra(Config.RICH_TEXT_RESULT);
                    addText(richTextResult);
                    break;
                case Config.SELECT_MAP:
                    LatLng latLng = data.getParcelableExtra(Config.MAP_RESULT_LOCATION);
                    String position = latLng.latitude + "," + latLng.longitude;
                    String map = data.getStringExtra(Config.MAP_RESULT_STRING);
                    addLocation(position, map);
                    break;
            }
        }
        if (requestCode == Config.RECORD) {
            if (resultCode == RESULT_OK) {
                showLoading("正在收藏中");
                totalTime = data.getStringExtra("time");
                totalTime = totalTime.substring(0, totalTime.lastIndexOf("."));
                String[] split = totalTime.split(":");
                totalTime = (Integer.parseInt(split[0]) * 60 * 60 + Integer.parseInt(split[1]) * 60 + Integer.parseInt(split[2])) + "";
                AndroidAudioConverter.with(CollectionActivity.this)
                        .setFile(new File(AUDIO_FILE_PATH))
                        .setFormat(AudioFormat.MP3)
                        .setCallback(new IConvertCallback() {
                            @Override
                            public void onSuccess(File file) {
                                audioSize = new File(AUDIO_FILE_PATH_MP3).length() / 1024 + "kb";
                                addAudio();
                            }

                            @Override
                            public void onFailure(Exception e) {
                                System.out.println(e);
                                hideLoading("收藏失败");
                            }
                        })
                        .convert();
            } else if (resultCode == RESULT_CANCELED) {
            }
        }
        if (requestCode == Config.SELECT_COLLECTION_GROUP && resultCode == Config.SUCCESS) {
            group = data.getIntExtra("id", 0);
            String name = data.getStringExtra("name");
            initTopTitleBar(View.VISIBLE, name);
            pageNum = 0;
            mvcHelper.refresh();
        }
        if (requestCode == Config.MOVE_COLLECTION_GROUP && resultCode == Config.SUCCESS) {
            int group = data.getIntExtra("id", 0);
            moveGroup(group);
        }
    }

    private void moveGroup(int group) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.MOVE_COLLECTION_GROUP)
                .params("owner", user.getGuid())
                .params("collections", slimAdapterEx.getDataItem(movePos).getCollection().getId())
                .params("groupid", group)
                .execute(new DialogCallBack<HttpResult>(CollectionActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            pageNum = 0;
                            mvcHelper.refresh();
                        }
                    }
                });
    }

    private void addLocation(String position, String map) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_COLLECTION)
                .params("owner", AppConstent.user.getGuid())
                .params("type", 6)
                .params("position", position)
                .params("ccontent", map)
                .params("source", "我的收藏")
                .params("sourceOwner", AppConstent.user.getGuid())
                .execute(new DialogCallBack<HttpResult>(CollectionActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            pageNum = 0;
                            mvcHelper.refresh();
                        }
                    }
                });
    }

    private void addAudio() {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_COLLECTION)
                .params("owner", AppConstent.user.getGuid())
                .params("type", 5)
                .params("tel", AppConstent.user.getUname())
                .params("ccontent", totalTime + " \" " + audioSize)
                .params("source", "我的收藏")
                .params("sourceOwner", AppConstent.user.getGuid())
                .params("file", new File(AUDIO_FILE_PATH_MP3))
                .execute(new DialogCallBack<HttpResult>(CollectionActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            pageNum = 0;
                            mvcHelper.refresh();

                        }
                    }
                });
    }

    private void addText(RichTextResult richTextResult) {
        int type;
        if (TextUtils.isEmpty(richTextResult.getContent())) {
            type = 1;
        } else if (richTextResult.getSelectMedia() == null || richTextResult.getSelectMedia().size() == 0) {
            type = 2;
        } else if (richTextResult.getSelectMedia().size() == 1) {
            type = 3;
        } else {
            type = 4;
        }
        PostRequest<HttpResult> params = OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_COLLECTION)
                .params("owner", AppConstent.user.getGuid())
                .params("type", type)
                .params("title", "我的收藏")
                .params("tel", AppConstent.user.getUname())
                .params("source", "我的收藏")
                .params("sourceOwner", AppConstent.user.getGuid())
                .params("ccontent", richTextResult.getContent());
        if (richTextResult != null) {
            List<LocalMedia> selectMedia = richTextResult.getSelectMedia();
            for (int i = 0, lenth = selectMedia.size(); i < lenth; i++) {
                params.params("file" + i, new File(selectMedia.get(i).isCompressed() ? selectMedia.get(i).getCompressPath() : selectMedia.get(i).getPath()));
            }
        }
        params.execute(new DialogCallBack<HttpResult>(CollectionActivity.this) {
            @Override
            public void onSuccess(Response<HttpResult> response) {
                HttpResult httpResult = response.body();
                ToastUtil.showShort(httpResult.getMsg());
                if (httpResult.getStatus() == 200) {
                    pageNum = 0;
                    mvcHelper.refresh();
                }
            }
        });
    }
}
